using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Ruminate.GUI.Framework;
using Ruminate.Utils;

namespace Ruminate.GUI.Content {

    public class TextRenderer : Renderer {

        public Texture2D Cursor { get; set; }
        public SpriteFont Font { get; set; }
        public Color Color { get; set; }

        public TextRenderer(SpriteFont font, Texture2D cursor, Color color, RuminateGUI gui) : base(gui, null) {

            Cursor = cursor;
            Font = font;
            Color = color;
        }

        public Texture2D Render(string value) {

            var baker = new TextureBaker(GraphicsDevice, Font.MeasureString(value), RenderState.Fill);

            baker.DrawString(Font, value, Vector2.Zero, Color);

            return baker.GetTexture();
        }

        public Texture2D Render(Point renderSize, string value, TextHorizontal h, TextVertical v) {            

            var container = renderSize.ToVector2();
            var location = Vector2.Zero;
            var size = Font.MeasureString(value);

            var baker = new TextureBaker(GraphicsDevice, container, RenderState.Fill);

            switch (h) {
                case TextHorizontal.CenterAligned:
                    location.X = (container.X - size.X) / 2;
                    break;
                case TextHorizontal.RightAligned:
                    location.X = container.X - size.X;
                    break;
            }

            switch (v) {
                case TextVertical.CenterAligned:
                    location.Y = (container.Y - size.Y) / 2;
                    break;
                case TextVertical.BottomAligned:
                    location.Y = container.Y - size.Y;
                    break;
            }

            baker.DrawString(Font, value, location, Color);

            return baker.GetTexture();
        }
    }
}
